from AccessControl import ClassSecurityInfo
from Globals import InitializeClass
from Products.KnowledgeEngine.Utils.CallbackIterator import CallbackIterator

class DisplayItem:
    """ An item in a display list """
    
    security=ClassSecurityInfo()
    security.setDefaultAccess('allow')
    
    def __init__( key, columns, disabled=False):
        self.key = key
        self.columns = columns
        self.disabled = disabled
        
    def getKey(self):
        return self.key
    
    def getColumns(self):
        return self.columns
    
    def isDisabled(self):
        return self.isDisabled
    
InitializeClass(DisplayItem)    
        
       
        
    

class DisplayList:
    
    def __init__(self, source): 
        self.source = source
        
    def __iter__(self):
        return iter(self.items())        
        
    def items(self):
        return CallbackIterator( self.source, self.returnRow )     
            
    def keys(self):
        return CallbackIterator( self.items(), lambda x:x[0] )
                        
    def values(self):
        return CallbackIterator( self.items(), lambda x:x[1] )
                
    def returnRow(self, row):
        """ 
            Return a key=>value tuple for the specified row 
            (Used as a callback by CallbackIterator)
        """
     
        if type(row) is tuple and len(row) == 2:
            return row
        
        elif type(row) in (str, unicode):
            key = row.replace(' ','_')
            return key, row
        
        else:
            return row, row            
        
        